#ifndef _USECONNECTIONPOINT_HELPER_H_
#define _USECONNECTIONPOINT_HELPER_H_

#include <dlcom/conntopoint/connectionpoint.h>
#include <vector>

namespace by {

template<class iid>
class UseConnectPoint
{
public:
	com_ptr<IConnectionPoint> m_pConnectPoint;
	DWORD	m_dwConnectPointCookie;
	BOOL	m_bConnected;

	UseConnectPoint():m_dwConnectPointCookie(-1),m_bConnected(FALSE)
	{

	};
	~UseConnectPoint()
	{
		DisConnect();
	}

	HRESULT Connect(IBase* pConnectionPointContainer, IBase* pThis)
	{
		com_ptr<IConnectionPointContainer> pCPC(pConnectionPointContainer);
		RASSERT(pCPC, E_FAIL);

		RFAILED(pCPC->FindConnectionPoint(_uuidof(iid), (IConnectionPoint**)&m_pConnectPoint));
		return Connect(pThis);
	}

	HRESULT Connect(IBase* pThis)
	{
		RASSERT(m_pConnectPoint, E_FAIL);
		
		com_ptr<iid> pMCP(pThis);
		RFAILED(m_pConnectPoint->Advise(pMCP, &m_dwConnectPointCookie));

		m_bConnected = TRUE;
		return S_OK;
	}

	HRESULT DisConnect()
	{
		if(m_pConnectPoint)
		{
			if(m_bConnected)
				m_pConnectPoint->Unadvise(m_dwConnectPointCookie);
			m_pConnectPoint = INULL;
		}

		m_bConnected = FALSE;
		return FALSE;
	}
};

} 

#endif 